﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using PServiceBus.Core.Logger;

namespace PServiceBus.Core.Runtime {
    public static class MethodHelper {
        public static Thread NewThread(Action action, ApartmentState state =  ApartmentState.MTA,
            string threadName = null) {
            var thread = new Thread(new ThreadStart(action));
            if (threadName != null)
                thread.Name = threadName;
            thread.IsBackground = true;
            thread.SetApartmentState(state);
            thread.Start();
            return thread;
        }
        public static Exception Try(Action action) {
            Exception ex = null;
            try { action(); } 
            catch (ThreadAbortException) { Thread.ResetAbort(); } 
            catch (Exception e) { ex = e; }
            return ex;
        }
        public static void TryLog(Action action) {
            ESBLogger.Log(Try(action));
        }
    }
}
